﻿Vue.directive('validation', {
    bind: function (el, binding, vnode) {
        var $el = $(el);
        var validation = binding.value;
                
        validation.validate = function () {
            var i = 0, prop;
            var isValid = true;
            if (validation.validators) {                
                for (prop in validation.validators) {
                    if (validation.validators.hasOwnProperty(prop) && !validation.validators[prop].validate())
                        isValid = false;
                }
            }            
            return isValid;
        };
    }
});

Vue.directive('required', {
    bind: function (el, binding) {        
        var $el = $(el);
        var validation = binding.value;
        if (!validation.validators)
            validation.validators = [];

        validation.validators[$el.attr("id")] = {
            validate: function () {
            var value = $el.val();               
            if (value === "") {
                $el.parent().addClass("has-danger");
                return false;
            }
            else {
                $el.parent().removeClass("has-danger");
                return true;
            }
        }};        
    }
});